package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// Map 设备导航地图
type DeviceMap struct {
	ID        string  `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	MapName   string  `json:"map_name" gorm:"column:map_name;type:string;size:100;index:map_name;comment:地图名称"`
	RobotID   string  `json:"robot_id" gorm:"column:robot_id;type:string;size:40;index:robot_id;comment:机器人id"`
	MapMd5    string  `json:"map_md5,omitempty" gorm:"column:map_md5;type:string;size:40;index:map_md5;comment:地图数据md5"`
	MapExMd5  string  `json:"map_ex_md5,omitempty" gorm:"column:map_ex_md5;type:string;size:40;index:map_ex_md5;comment:地图扩展数据md5"`
	Width     float64 `json:"width" gorm:"column:width;type:float;size:64;comment:地图原始宽度"`
	Height    float64 `json:"height" gorm:"column:height;type:float;size:64;comment:地图原始高度"`
	R11       float32 `json:"r11" gorm:"column:r11;type:float;size:32;default:0;comment:转换矩阵"`
	R12       float32 `json:"r12" gorm:"column:r12;type:float;size:32;default:0;comment:转换矩阵"`
	R21       float32 `json:"r21" gorm:"column:r21;type:float;size:32;default:0;comment:转换矩阵"`
	R22       float32 `json:"r22" gorm:"column:r22;type:float;size:32;default:0;comment:转换矩阵"`
	Tx        float32 `json:"tx" gorm:"column:tx;type:float;size:32;default:0;comment:转换矩阵"`
	Ty        float32 `json:"ty" gorm:"column:ty;type:float;size:32;default:0;comment:转换矩阵"`
	DeletedAt int64   `json:"deleted_at" gorm:"column:deleted_at;index:deleted_at;type:int;size:64;default:0;comment:删除时间"`
	CreatedAt int64   `json:"created_at,omitempty" gorm:"autoCreateTime;column:created_at;type:int;size:64;comment:创建时间"`
	UpdatedAt int64   `json:"updated_at,omitempty" gorm:"autoUpdateTime;column:updated_at;type:int;size:64;comment:更新时间"`
	UsedAt    int64   `json:"used_at,omitempty" gorm:"column:used_at;type:int;size:64;comment:最近被使用时间"`
}

// TableName 指定表名
func (DeviceMap) TableName() string {
	return "map"
}

// BeforeCreate 自增id
func (m *DeviceMap) BeforeCreate(tx *gorm.DB) (err error) {
	if m.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	m.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &DeviceMap{})
}
